package com.sendtion.xrichtextdemo.adapter;

import com.sendtion.xrichtextdemo.ResourceTable;
import com.sendtion.xrichtextdemo.bean.Note;

import com.sendtion.xrichtextdemo.utils.ItemClickCallBack;
import com.sendtion.xrichtextdemo.utils.ScreenUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * MyNoteListAdapter
 *
 * @since 2021-06-22
 */
public class MyNoteListAdapter extends BaseItemProvider implements Component.ClickedListener,
        Component.LongClickedListener {
    private AbilitySlice mContext;
    private List<Note> mNotes;
    private OnRecyclerViewItemClickListener mOnItemClickListener;
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener;
    private ItemClickCallBack itemClickCallBack;

    /**
     * MyNoteListAdapter
     *
     * @param context
     * @param noteList
     */
    public MyNoteListAdapter(AbilitySlice context, List<Note> noteList, ItemClickCallBack itemClickCallBack) {
        this.mContext = context;
        this.mNotes = noteList;
        this.itemClickCallBack = itemClickCallBack;
    }

    @Override
    public void onClick(Component component) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(component, (Note) component.getTag());
        }
    }

    @Override
    public void onLongClicked(Component component) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick(component, (Note) component.getTag());
        }
    }

    /**
     * OnRecyclerViewItemClickListener
     *
     * @since 2021-06-22
     */
    public interface OnRecyclerViewItemClickListener {
        /**
         * onItemClick
         *
         * @param view
         * @param note
         */
        void onItemClick(Component view, Note note);
    }

    /**
     * setOnItemClickListener
     *
     * @param listener
     */
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * OnRecyclerViewItemLongClickListener
     *
     * @since 2021-06-22
     */
    public interface OnRecyclerViewItemLongClickListener {
        /**
         * onItemLongClick
         *
         * @param view
         * @param note
         */
        void onItemLongClick(Component view, Note note);
    }

    /**
     * setOnItemLongClickListener
     *
     * @param listener
     */
    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public int getCount() {
        if (mNotes != null && mNotes.size() > 0) {
            return mNotes.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (mNotes != null && i >= 0 && i < mNotes.size()) {
            return mNotes.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt;
        ViewHolder viewHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext)
                    .parse(ResourceTable.Layout_list_item_note, null, false);
            viewHolder = new ViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (ViewHolder) cpt.getTag();
        }
        cpt.setClickedListener(this);
        cpt.setLongClickedListener(this);
        viewHolder.tvlisttitle.setText(mNotes.get(i).getTitle());
        viewHolder.tvlistsummary.setText(mNotes.get(i).getContent());
        viewHolder.tvlisttime.setText(mNotes.get(i).getCreateTime());
        viewHolder.tvlistgroup.setText("默认笔记");
        viewHolder.cardviewnote.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                show(mContext, "确定删除笔记？", "确认", viewHolder.tvlisttitle);
            }
        });
        return cpt;
    }

    /**
     * 显示方法
     *
     * @param context
     * @param title
     * @param content
     * @param text
     */
    private void show(Context context, String title, String content, Text text) {
        ComponentContainer customToastLayout = (ComponentContainer) LayoutScatter
                .getInstance(context).parse(ResourceTable.Layout_dialog_layout, null, false);
        Text mTitle = (Text) customToastLayout.findComponentById(ResourceTable.Id_tv_title);
        Text mContent = (Text) customToastLayout.findComponentById(ResourceTable.Id_tv_content);

        mTitle.setText(title);
        mContent.setText(content);

        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        customToastLayout.setLayoutConfig(layoutConfig);

        CommonDialog toastDialog = new CommonDialog(context);
        toastDialog.setContentCustomComponent(customToastLayout);
        toastDialog.setAutoClosable(true);
        toastDialog.setSize(ScreenUtils.getDisplayWidth(context) / 7 * 6,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();

        mContent.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                itemClickCallBack.itemClick(text);
                toastDialog.destroy();
            }
        });
        Text cancel = (Text) customToastLayout.findComponentById(ResourceTable.Id_tv_cancel);
        cancel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                toastDialog.destroy();
            }
        });
    }

    /**
     * ViewHolder
     *
     * @since 2021-06-22
     */
    public static class ViewHolder {
        Text tvlisttitle;
        Text tvlistsummary;
        Text tvlisttime;
        Text tvlistgroup;
        DirectionalLayout cardviewnote;

        /**
         * ViewHolder
         *
         * @param component
         */
        public ViewHolder(Component component) {
            tvlisttitle = (Text) component.findComponentById(ResourceTable.Id_tv_list_title);
            tvlistsummary = (Text) component.findComponentById(ResourceTable.Id_tv_list_summary);
            tvlisttime = (Text) component.findComponentById(ResourceTable.Id_tv_list_time);
            tvlistgroup = (Text) component.findComponentById(ResourceTable.Id_tv_list_group);
            cardviewnote = (DirectionalLayout) component.findComponentById(ResourceTable.Id_card_view_note);
        }
    }
}
